package com.wenwu.controller.unmovable;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsBasic;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.common.utils.Status.AttachmentFrom;
import com.wenwu.service.BasicService;
import com.wenwu.service.CommonService;

/**
 * @project RelicsProject-web
 * @class BasicController.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年5月2日 下午2:30:52
 * @description 基本信息登记管理
 */
@Controller(value = "BasicController")
@RequestMapping(value = "/mgr/umbasic")
public class BasicController {
	Logger log = Logger.getLogger(this.getClass());
	// 用来创建json数据
	@Autowired
	private BasicService basicService;
	@Autowired
	private CommonService commonService;
	private JsonGenerator jsonGenerator = null;

	@RequestMapping(value = "/init")
	public String initUserPage() {
		log.info("进入基本信息管理页面...");
		return "umbasic/umbasiclist";
	}

	@RequestMapping(value = "/basicform")
	public String dictionaryform(Model model,
			@RequestParam(required = false, value = "basicId") Integer basicId)
			throws Exception {
		log.info("进入基本信息表单页面...");
		try {
			if (basicId != null) {// 查询对应的附件集合
				List<RelicsAttachment> attachments = commonService
						.selectAttachmentListByKey(basicId,AttachmentFrom.RELICS_BASIC_01);
				model.addAttribute("attachments", attachments);
			}
		} catch (Exception e) {
		}
		return "umbasic/umbasicform";
	}

	/**
	 * 添加不可移动文物基本信息
	 */
	@RequestMapping(value = "/addBasic")
	public void addBasic(RelicsBasic relicsBasic, HttpServletResponse response)
			throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		log.info("添加数据...");
		try {
			jsonGenerator.writeObject(basicService.addObj(relicsBasic));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage());
			jsonGenerator.writeObject(e.getMessage());
		}
	}

	@RequestMapping(value = "/showList")
	public @ResponseBody PaginationVO showList(RelicsBasic relicsBasic,
			HttpServletRequest request) throws Exception {
		PaginationVO pageVo = null;
		try {
			pageVo = PageUtils.getPaginationInfo(request);
			int totalRecord = basicService.selectTotalRecord(relicsBasic);// 查询符合条件的个数
			List<RelicsBasic> list = basicService.selectList(relicsBasic,
					pageVo);// 查询符合条件的数据
			pageVo.setList(list);// 将数据设置到PaginationVO对象中
			pageVo.setTotalRecord(totalRecord);
		} catch (Exception e) {
		}
		return pageVo;
	}

	@RequestMapping(value = "/showUpdate")
	public @ResponseBody RelicsBasic showUpdate(
			@RequestParam(required = false, value = "basicId") Integer basicId)
			throws Exception {
		RelicsBasic relicsBasic = null;
		try {
			if (basicId != null) {
				relicsBasic = basicService.selectObjByKey(basicId);
			}
		} catch (Exception e) {
		}
		return relicsBasic;
	}

	@RequestMapping(value = "/deleteInfo")
	public void deleteInfo(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(required = false, value = "key") String key)
			throws Exception {
		log.info("主键：" + key);
		if (key != null) {
			List<String> keys = new ArrayList<String>();
			String[] objs = key.split(",");
			for (String k : objs) {
				keys.add(k);
			}
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			try {
				int result = basicService.deleteByKey(keys);
				jsonGenerator.writeObject("成功删除" + result + "条数据");
			} catch (Exception e) {
				jsonGenerator.writeObject("删除数据失败");
			}
		}
	}
}
